﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Musicman.Common.Entities;
using Musicman.Business;

namespace MusicmanWeb.USER
{
    public partial class UserHistory : MusicmanPage
    {
        override protected void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            long personID = -1;
            if (String.IsNullOrEmpty(Request["personid"]))
            {
                Int64.TryParse(User.Identity.Name, out personID);
            }
            else
            {
                Int64.TryParse(Request["personid"], out personID);
            }
            List<HistoryFile> history = GetUserHistory(personID);
            this.UserHistoryGV.DataSource = history;
            this.UserHistoryGV.DataBind();
        }

        private List<HistoryFile> GetUserHistory(long personID)
        {
            if (Cache["personid"] == null || (long)Cache["personid"] != personID)
            {
                Cache["personid"] = personID;
            }
            if (Cache["userhistory"] == null)
            {
                List<HistoryFile> users = SearchFileManager.Instance.LoadUserHistory(personID);
                Cache.Insert("userhistory", users,
                    new System.Web.Caching.CacheDependency(new string[] { }, new string[] { "personid" }),
                    DateTime.Now.AddMinutes(3), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            return Cache["userhistory"] as List<HistoryFile>;
        }

        protected void UserHistoryGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView gv = sender as GridView;
            long personID = -1;
            if (String.IsNullOrEmpty(Request["personid"]))
            {
                Int64.TryParse(User.Identity.Name, out personID);
            }
            else
            {
                Int64.TryParse(Request["personid"], out personID);
            }
            List<HistoryFile> files = GetUserHistory(personID);
            gv.DataSource = files;
            gv.PageIndex = e.NewPageIndex;
            gv.DataBind();
        }
    }
}
